<?php
/*
 * File name : SignPledge.php
 * Description : Make a pledge based donation.
 * Created By : Pratibha Awasthi
 */

include_once "basePage.php";

class SignPledge extends basePage{
	
	public function sign_pledge(){

			$frequency = $_POST['frequency'];
			session_start();
			$userid = $_SESSION['userid'];
			$start = $_POST['start_date'];
			
			$end = $_POST['end_date'];
			$amount = $_POST['amount'];
			$agency = $_POST['agency'];
			
			
			$con = mysql_connect('localhost','pa_user','secret_password');
			if (!$con){
		  		die('Could not connect: ' . mysql_error());
		  	}
		  
			mysql_select_db("TGS_GM_RESERVATION", $con);
			
			$agencyid = mysql_query("SELECT ID FROM TGS_GM_RESERVATION.tblagency where Name = '{$agency}'");
			$agencyid =  mysql_fetch_array($agencyid);
			$agencyid = $agencyid['ID'];
			
			$userid = mysql_query("SELECT SubscriberID FROM TGS_GM_RESERVATION.tblsubscriber where UserID = '{$userid}'");
			$userid =  mysql_fetch_array($userid);
			$subsid = $userid['SubscriberID'];
			
			$frequency = mysql_query("SELECT FrequencyID FROM TGS_GM_RESERVATION.lkpfrequency where FrequencyType = '{$frequency}'");
			$frequencyid = mysql_fetch_array($frequency);
			$frequencyid = $frequencyid['FrequencyID'];
			
			$result = mysql_query("SELECT * FROM TGS_GM_RESERVATION.tblsubscriberpledge where AgencyID = '{$agencyid}' and tblSubscriber_SubscriberID = '{$subsid}' and StartDate = 'DATE : {$start}' and EndDate = 'DATE : {$end}' and Dollars = '{$amount}'");
			$row = mysql_fetch_array($result);
		 	
			//var_dump($row);
			
			if($frequency == '0' || $agency == '0' || $start == NULL || $end == NULL || $amount == NULL || $agency == NULL){
				echo "<h3>Please enter all the details required for a pledge based donation.</h3>";
				echo "<input type = 'submit' name = 'submit' value = 'Back' onclick= 'history.go(-1);'>";
			}
			else if($row['AgencyID'] == NULL){		
				
				$rowcount_result = mysql_query("SELECT * FROM TGS_GM_RESERVATION.tblsubscriberpledge");
				$num_rows = mysql_num_rows($rowcount_result);
				
				$rowno = $num_rows + 1;
				
				$start_timestamp = strtotime($start);
				$start_timestamp = date("Y-m-d H:i:s", $start_timestamp);

				
				$mysqldate = date( 'Y-m-d H:i:s', $start );
				//var_dump($mysqldate);
				$phpdate = strtotime( $mysqldate );
				//var_dump($phpdate);
				
				$end_timestamp = strtotime($end);
					
				$end_timestamp = date("Y-m-d H:i:s", $end_timestamp);
				
				mysql_query("INSERT INTO TGS_GM_RESERVATION.tblsubscriberpledge(SubscriberPledgeID, StartDate ,EndDate, Dollars, FrequencyID, tblSubscriber_SubscriberID, AgencyID) VALUES ({$rowno}, '{$start_timestamp}', '{$end_timestamp}', '{$amount}', '{$frequencyid}','{$subsid}','{$agencyid}')");
				echo "<h3> Congratulations !!!! <br/> You have successfully subscribed to the Pledge based donation !!!!</h3>";
				echo "<input type = 'submit' name = 'submit' value = 'Back' onclick= 'history.go(-1);'>";	
			}
			else 
				echo "<h3> You already have a similar pledge based donation.</h3>";			
	}
}


$page = new SignPledge();
$page->header();
$page->top_menu_donor();
$page->sign_pledge();
$page->footer();

?>
